﻿using System;
using Jay.Sc2.Bof.Lib.State;

namespace Jay.Sc2.Bof.Lib.Action.Terran
{
	public class ActionProduceNuke : ActionProduceTerran
	{
		public static ActionProduceNuke Instance = new ActionProduceNuke();

		public ActionProduceNuke() : base(60, 100, 100, 0) { }

		public override int AvailableProducer(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			return Math.Min(state.GhostAcademy.AvailableForUpgrade, state.GhostAcademy.Count - (state.Nuke + state.Nuke_Producing));
		}

		public override bool HaveTechnology(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			return state.GhostAcademy.Count > 0 && state.Factory.Count + state.FactoryReactor.Count + state.FactoryTechLab.Count > 0;
		}

		public override bool IsUseless(IBuildOrder bo, IGoal goal)
		{
			TerranState current = bo.State as TerranState;
			if (base.IsUseless(bo, goal)) return true;
			int total = 0;
			foreach (IState state in goal)
			{
				TerranState s = state as TerranState;
				total += s.Nuke;
			}
			return total <= current.Nuke + current.Nuke_Producing;
		}

		public override bool IsInvalid(Jay.Sc2.Bof.Lib.State.IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			return base.IsInvalid(bo) || state.GhostAcademy.Total <= (state.Nuke + state.Nuke_Producing) || state.Factory.Total + state.FactoryReactor.Total + state.FactoryTechLab.Total == 0;
		}

		public override void Execute(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			base.Execute(bo);
			state.GhostAcademy.Upgrade++;
			state.Nuke_Producing++;
			bo.AddAction(state.Time + BuildTime, new ActionGeneric("ActionSpawnNuke", (ActionGeneric.ActionExecuteDelegate)delegate(IBuildOrder ibo)
			{
				TerranState ibostate = ibo.State as TerranState;
				ibostate.GhostAcademy.Upgrade--;
				ibostate.Nuke++;
				ibostate.Nuke_Producing--;
			}));
		}

		private IAction[,] m_Requirements;
		public override IAction[,] Requirements
		{
			get
			{
				if (m_Requirements == null)
					m_Requirements = new IAction[,] { { ActionProduceFactory.Instance, ActionProduceGhostAcademy.Instance } };
				return m_Requirements;
			}
		}
	}
}
